{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#* \n",
    "\n",
    "\n",
    "\n",
    "import cv2 as cv\n",
    "import os\n",
    "\n",
    "\n",
    "from os.path import join,exists, split\n",
    "os.chdir('/data/xusc/exp/topictrack-bee')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "datasets/antmove/test/ant04/gt/gt.txt\n",
      "datasets/antmove/test/ant03/gt/gt.txt\n"
     ]
    }
   ],
   "source": [
    "# 1. 进入到train下，\n",
    "# 2. 进入到每个序列\n",
    "\n",
    "\"\"\"\n",
    "resource : https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/127531790\n",
    "\"\"\"\n",
    "\n",
    "# seq = ['train','test']\n",
    "seq = ['test']\n",
    "dataset_name = 'antmove'\n",
    "for s in seq:\n",
    "    file_dir = 'datasets/%s/'%(dataset_name)+s\n",
    "    list1 = os.listdir(file_dir)\n",
    "\n",
    "    target_dir = \"datasets/%s_test/%s\"%(dataset_name,s)\n",
    "\n",
    "    if not exists(target_dir):\n",
    "        os.makedirs(target_dir)\n",
    "\n",
    "    \n",
    "    for sen in os.listdir(file_dir):\n",
    "        gt_file = os.path.join(file_dir, sen, 'gt', 'gt.txt')\n",
    "        print(gt_file)\n",
    "        f = open(gt_file,'r')\n",
    "        gt_line = f.readlines()\n",
    "        for i in gt_line:\n",
    "            gt1 = i.split(',')\n",
    "            img_dir = os.path.join(file_dir, sen, 'img1', gt1[0]+'.jpg')\n",
    "            img = cv.imread(img_dir)\n",
    "            img_crop = img[int(float(gt1[3])):int(float(gt1[3]))+int(float(gt1[5])), int(float(gt1[2])):int(float(gt1[2]))+int(float(gt1[4]))]\n",
    "\n",
    "            try:\n",
    "                cv.imwrite('datasets/%s_test/%s/%04d_c1s%d_%06d_%02d.jpg'%(dataset_name,s, \\\n",
    "                    int(float(gt1[1])), int(sen[-2:]), int(float(gt1[0])), int(float(gt1[1]))), img_crop)\n",
    "\n",
    "            except Exception as e :\n",
    "\n",
    "                print(e)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1676\n",
      "[373, 3777, 5580, 4831, 5304, 2863, 151, 4933, 1977, 3768, 5161, 3542, 3942, 3456, 4573, 2528, 41, 4681, 2865, 659, 5407, 696, 4304, 2771, 5326, 4527, 3751, 109, 3248, 3206, 5036, 1787, 3921, 3510, 3252, 1564, 3308, 2385, 3846, 4529, 3224, 4187, 2024, 1418, 5233, 657, 5065, 2064, 2507, 3348, 51, 626, 5443, 1462, 1542, 4697, 2006, 1711, 203, 3583, 1296, 4815, 1775, 2410, 5062, 4462, 3021, 2754, 2422, 1107, 1218, 4936, 4200, 2033, 3491, 4203, 3618, 257, 4601, 2331, 1215, 2682, 1831, 1576, 522, 3187, 715, 1037, 2418, 4103, 3975, 5436, 2225, 302, 4603, 3745, 1955, 1991, 2636, 5113, 4451, 3081, 2425, 3160, 5543, 157, 616, 5559, 1947, 5196, 3648, 681, 5369, 4708, 2826, 3734, 2437, 4438, 1262, 235, 80, 3347, 1163, 4846, 5044, 538, 3928, 5502, 1954, 4381, 2370, 4577, 2623, 1172, 2621, 722, 321, 31, 589, 5156, 141, 4867, 4154, 3840, 3066, 4911, 1809, 593, 2479, 4195, 1245, 2460, 17, 88, 2003, 1584, 2229, 1634, 3166, 2998, 2757, 4970, 5232, 3094, 1832, 530, 3806, 4500, 531, 4799, 5154, 1516, 574, 4336, 4664, 4866, 1722, 483, 1604, 3235, 4726, 4467, 928, 2489, 2608, 1266, 5193, 1720, 3898, 4385, 4029, 5015, 1810, 54, 4404, 1305, 4295, 5501, 1318, 2928, 877, 2143, 2975, 5249, 1440, 1950, 2506, 224, 1917, 2859, 2141, 2022, 3885, 1522, 2100, 2405, 942, 4985, 236, 155, 1938, 1231, 3620, 603, 874, 5386, 4619, 3323, 3189, 1837, 4626, 4882, 5107, 4791, 1114, 4126, 800, 886, 5457, 2782, 634, 1758, 1073, 49, 4962, 5250, 2683, 1087, 848, 4119, 4464, 1157, 1150, 4946, 4344, 3472, 4444, 2836, 3675, 4758, 508, 2951, 1203, 1104, 364, 1132, 761, 2404, 3212, 2839, 2825, 5061, 584, 1096, 4274, 910, 4615, 1543, 1265, 3197, 4508, 1282, 5047, 3600, 3969, 216, 2209, 4513, 3940, 2239, 3548, 1659, 5284, 3857, 2670, 319, 64, 3567, 3091, 2765, 1972, 5529, 1159, 455, 4332, 3073, 1957, 3421, 4822, 351, 4943, 5452, 3963, 2823, 1687, 3314, 4452, 3471, 1210, 1694, 1405, 2896, 3770, 4827, 617, 3874, 1380, 4349, 4111, 4862, 4125, 3650, 83, 4045, 5372, 762, 4924, 4565, 349, 5434, 3053, 1464, 2729, 1590, 320, 4027, 303, 3865, 3334, 3673, 4796, 2894, 4240, 246, 1867, 2833, 5135, 339, 2862, 3784, 3609, 2590, 940, 4569, 3625, 2600, 4112, 1009, 5531, 311, 1308, 665, 4092, 1025, 5356, 5534, 1309, 4922, 4233, 3867, 1019, 4476, 2681, 1463, 4495, 4579, 3354, 3506, 91, 1901, 3474, 4583, 2473, 494, 3259, 3202, 3521, 1879, 4876, 3236, 1337, 3622, 5499, 5230, 5163, 4748, 1244, 437, 1521, 892, 4278, 3787, 5415, 4559, 5417, 4614, 4390, 3608, 4535, 1702, 3843, 3425, 3578, 101, 605, 3337, 2264, 1916, 280, 2070, 5018, 2154, 1102, 3194, 1583, 773, 190, 1074, 2490, 3573, 2659, 191, 3575, 1975, 4395, 3451, 4958, 4399, 1973, 4311, 716, 2591, 1772, 3953, 5187, 5266, 4072, 21, 2561, 2315, 3543, 1030, 35, 2933, 4257, 1589, 125, 3391, 1918, 3427, 2142, 2804, 3834, 1656, 3872, 1519, 1046, 3964, 4605, 2322, 222, 1652, 2794, 2493, 3683, 4436, 1143, 3983, 660, 5422, 3351, 4305, 2559, 3682, 2284, 4769, 2690, 4795, 4666, 2269, 1233, 318, 4705, 1530, 2151, 4706, 4310, 5258, 5520, 3379, 496, 1212, 5581, 1286, 4542, 2672, 160, 2169, 5508, 4864, 3280, 1985, 5075, 1261, 4968, 4198, 4443, 4662, 5205, 2767, 3503, 4704, 4106, 4718, 5220, 3541, 1705, 2183, 4070, 2574, 5451, 1949, 1609, 3038, 3147, 2913, 201, 702, 5068, 4942, 5426, 445, 4879, 4836, 5114, 752, 673, 1561, 1926, 4633, 3093, 3414, 5238, 804, 1065, 4997, 3644, 2577, 908, 2286, 4716, 1185, 3222, 3706, 3159, 1335, 487, 4189, 3725, 4485, 1216, 1838, 1738, 5067, 1886, 4186, 2924, 2211, 3992, 3369, 307, 1059, 3000, 2037, 3688, 1693, 1489, 4929, 4667, 2392, 514, 1402, 255, 1699, 4849, 1070, 3968, 4590, 3518, 3703, 3585, 986, 1782, 2401, 546, 3181, 972, 1230, 1386, 3228, 4153, 3645, 30, 4548, 3365, 4335, 1835, 478, 4411, 3449, 585, 4701, 2194, 4617, 5176, 2583, 4835, 2967, 2491, 3966, 529, 2908, 4539, 3358, 2309, 1833, 2560, 506, 1963, 5231, 1288, 3118, 1766, 5579, 3256, 3836, 4982, 2770, 614, 3428, 2798, 5439, 5433, 4853, 2802, 3973, 4558, 5189, 1088, 611, 4905, 4641, 2984, 2190, 1423, 5393, 1353, 3802, 2688, 1388, 2675, 3109, 3504, 1174, 27, 2503, 3129, 5432, 2809, 5424, 4780, 4372, 1207, 331, 5583, 3151, 1961, 3035, 4885, 460, 4178, 5109, 5481, 401, 2835, 3295, 3764, 4093, 1625, 5411, 2828, 5008, 1027, 1646, 7, 2031, 4602, 1754, 75, 5560, 3243, 2890, 684, 3221, 4717, 1177, 852, 3597, 898, 2650, 4757, 1247, 1929, 4117, 706, 4044, 291, 2478, 4808, 317, 3511, 3664, 3623, 2738, 3010, 688, 2415, 2436, 1930, 5093, 1858, 3004, 4597, 887, 2304, 3401, 539, 5289, 3105, 1900, 1909, 37, 3043, 5079, 1896, 3007, 4553, 4722, 2837, 838, 2276, 2706, 5572, 4525, 3155, 2744, 4889, 1873, 2161, 444, 2840, 3997, 5330, 4881, 5340, 4421, 1221, 149, 2065, 846, 561, 1133, 2312, 540, 4952, 1710, 3758, 1854, 2423, 3078, 960, 1189, 3318, 1180, 2232, 2784, 1692, 4370, 4670, 2855, 3748, 424, 921, 432, 3070, 324, 2822, 2971, 1859, 3473, 3848, 239, 1219, 134, 3209, 1293, 2522, 3361, 2184, 5392, 1437, 3972, 4989, 2897, 1075, 2077, 723, 2947, 2566, 4040, 5152, 3640, 2926, 1344, 1562, 1329, 1152, 3175, 2741, 1866, 4652, 3482, 5006, 1367, 2851, 210, 1840, 1760, 118, 2311, 3298, 3668, 1378, 3562, 2259, 5328, 4286, 695, 3429, 3065, 2197, 22, 1887, 58, 2994, 4737, 2698, 4000, 3352, 3255, 3822, 1636, 3887, 3110, 3642, 3949, 1785, 1847, 3665, 3027, 1008, 4159, 1398, 2480, 3607, 2021, 768, 3269, 378, 4392, 2847, 5480, 4355, 3413, 687, 1162, 3862, 2484, 3203, 220, 2328, 3733, 4764, 3437, 2377, 361, 2461, 2748, 5362, 350, 176, 3114, 974, 3954, 5413, 5243, 2813, 1366, 1496, 630, 5458, 3502, 5115, 3177, 2178, 3407, 5570, 3809, 5059, 3306, 4426, 347, 2494, 5459, 4339, 2793, 3912, 434, 5435, 4642, 3387, 2553, 4747, 1451, 3011, 4767, 1110, 3646, 2135, 465, 5174, 4091, 3805, 2067, 666, 1003, 1138, 2262, 179, 4628, 2934, 3207, 795, 4965, 2255, 2925, 4961, 778, 2270, 4122, 2842, 3939, 1595, 3284, 241, 4287, 1888, 2914, 5370, 2674, 1654, 743, 2110, 2694, 2351, 3895, 1281, 169, 1748, 3281, 3234, 914, 2203, 3681, 4630, 881, 185, 3782, 3265, 5033, 1549, 2399, 2597, 4459, 5027, 1232, 3426, 2523, 4480, 4368, 2527, 5342, 4721, 1236, 2175, 1345, 4557, 3009, 5245, 503, 1413, 2592, 707, 2168, 4223, 1935, 1881, 796, 4886, 2673, 2541, 3967, 4141, 2733, 4931, 4669, 844, 4275, 469, 3666, 3617, 3652, 464, 3434, 573, 3214, 5283, 458, 4249, 4074, 1294, 3713, 308, 2223, 3858, 5118, 3003, 4839, 1967, 4696, 891, 5503, 3399, 3376, 2265, 4700, 4698, 1453, 1280, 463, 5339, 542, 295, 1029, 4101, 470, 4963, 338, 4341, 5106, 2167, 2596, 6, 3647, 4800, 4851, 4926, 462, 2727, 5209, 5039, 2513, 2554, 250, 2474, 1649, 3678, 4776, 2705, 807, 2702, 1249, 2200, 4251, 3851, 985, 1275, 1076, 1401, 4104, 2193, 2383, 4300, 1387, 1791, 3037, 1166, 5467, 4037, 3219, 709, 2647, 2631, 3430, 1050, 1953, 4803, 2408, 68, 436, 5522, 3818, 2074, 1039, 3513, 1115, 96, 5277, 310, 433, 296, 3272, 5136, 5574, 5361, 1921, 3350, 2097, 5517, 4828, 3325, 1089, 4124, 5173, 899, 3048, 5454, 135, 1325, 894, 3068, 1730, 4761, 1871, 2537, 3343, 1570, 1303, 1020, 3637, 4135, 4264, 2321, 4149, 5088, 2182, 4334, 2834, 2245, 4693, 1726, 2498, 3371, 2509, 4682, 797, 5530, 2678, 3536, 1071, 588, 1746, 545, 1800, 533, 1593, 4192, 3796, 5144, 5506, 1697, 4531, 5247, 2054, 5401, 1507, 2181, 2878, 982, 3911, 40, 787, 1267, 825, 1761, 1414, 5493, 1727, 5087, 760, 4916, 2730, 2707, 4247, 3067, 2552, 2526, 4301, 2441, 1863, 1105, 2510, 4105, 2700, 1577, 4873, 1320, 5464, 2303, 2487, 2881, 2810, 3800, 4621, 449, 2289, 2268, 3893, 580, 2689, 2102, 4086, 2986, 3700, 865, 5137, 5333, 4252, 816, 549, 3690, 4469, 3935, 4875, 4068, 3196, 916, 3780, 5511, 1228, 4215, 4081, 3916, 1198, 495, 4676, 221, 4185, 680, 5404, 2053, 2861, 1553, 116, 3861, 425, 5222, 450, 3075, 4291, 4957, 178, 3962, 2045, 39, 3185, 123, 2222, 3468, 139, 4036, 4818, 2085, 987, 3032, 3423, 2388, 2445, 1878, 1770, 268, 1409, 1066, 4128, 4731, 3667, 1645, 2212, 312, 4131, 911, 4428, 1085, 3773, 5582, 970, 3484, 3917, 2376, 2236, 1940, 4999, 906, 5421, 1591, 3338, 188, 5157, 3393, 3686, 199, 227, 1812, 2332, 992, 4609, 4397, 481, 24, 5546, 5229, 5300, 371, 4206, 3724, 4474, 863, 5028, 342, 1944, 3201, 438, 48, 43, 765, 353, 428, 3779, 2004, 2140, 156, 2081, 4014, 2536, 4544, 1798, 623, 3691, 4594, 790, 3300, 1824, 3783, 4121, 4891, 3794, 820, 789, 2302, 1780, 1498, 2916, 3520, 187, 173, 4176, 2918, 4477, 2930, 5315, 5003, 3933, 290, 4947, 5320, 4069, 3804, 1739, 3137, 979, 2629, 1258, 2749, 2307, 1960, 1552, 431, 4865, 5492, 4134, 4912, 87, 4984, 793, 3059, 1302, 1062, 4707, 2764, 3111, 4647, 3240, 2697, 1122, 5198, 1260, 3655, 4337, 314, 3815, 1116, 3135, 2852, 4784, 3914, 4113, 831, 4458, 5445, 517, 3593, 3799, 4419, 858, 1688, 3829, 4909, 2676, 2339, 1211, 1369, 4668, 5303, 1350, 3057, 3908, 3619, 5487, 4575, 4774, 2288, 78, 3418, 1112, 4902, 4981, 3970, 1945, 3150, 3863, 1443, 1117, 2856, 759, 5165, 2812, 2455, 5096, 2838, 2875, 3171, 3850, 3448, 2462, 61, 3604, 4312, 500, 1825, 2785, 2310, 2384, 2348, 5185, 3406, 4954, 1993, 2703, 1811, 4346, 1078, 3140, 3016, 5576, 4267, 5406, 4440, 952, 1707, 3340, 3178, 5376, 131, 2029, 1959, 2960, 5255, 672, 4391, 2614, 4272, 999, 1300, 1241, 1728, 2130, 780, 2430, 5002, 175, 4437, 555, 2762, 2814, 954, 933, 648, 1472, 2943, 5295, 3097, 2752, 177, 4789, 2936, 2657, 650, 4038, 3460, 4775, 4183, 2917, 5325, 2446, 363, 3559, 4819, 1939, 3816, 4556, 828, 5272, 3886, 5099, 4417, 248, 951, 4292, 3279, 2457, 3705, 2974, 4689, 3959, 3124, 4343, 1912, 247, 4453, 3767, 2662, 5431, 1995, 269, 1529, 4309, 111, 1846, 5141, 355, 1579, 1225, 2753, 5172, 3907, 3769, 5188, 1545, 5256, 1914, 2497, 3158, 2426, 95, 4171, 4809, 5311, 2406, 5111, 5571, 2827, 918, 568, 3266, 4356, 1067, 3880, 5414, 3233, 4729, 4967, 4871, 270, 3424, 2132, 3305, 2340, 2417, 3671, 2889, 1924, 3321, 2048, 1458, 2152, 4338, 4202, 2016, 5180, 701, 4259, 4517, 2492, 3079, 5552, 4076, 2333, 750, 1857, 3979, 1566, 2389, 2146, 3808, 2929, 1597]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<__main__.QueryGenerator at 0x7f8dfe3438e0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#* generate query set \n",
    "\n",
    "\n",
    "\n",
    "import random\n",
    "import shutil\n",
    "\n",
    "tgt_path = 'datasets/%s_test/query'%(dataset_name)\n",
    "src_path = 'datasets/%s_test/bounding_box_test'%(dataset_name)\n",
    "\n",
    "\n",
    "class QueryGenerator:\n",
    "\n",
    "\n",
    "    def __init__(self, tgt ,src):\n",
    "\n",
    "        self.tgt = tgt\n",
    "\n",
    "        if not  exists(tgt_path):\n",
    "            os.makedirs(tgt_path)\n",
    "\n",
    "\n",
    "        self.src = src\n",
    "\n",
    "        self.obj_lib = os.listdir(src)\n",
    "\n",
    "        src_len = len(self.obj_lib)\n",
    "\n",
    "        \n",
    "        random_numbers = random.sample(range(src_len), int(src_len * 0.3))\n",
    "        print(int(src_len * 0.3))\n",
    "        print(random_numbers)\n",
    "\n",
    "\n",
    "        new_camera_id = 2\n",
    "        for idx in random_numbers:\n",
    "            img_info = self.obj_lib[idx].split('_')\n",
    "            # print('before',img_info)\n",
    "            camera_info = img_info[1]\n",
    "            new_camera_info = camera_info[0] + str(new_camera_id) + camera_info[2:] \n",
    "            img_info[1] = new_camera_info\n",
    "            # print('after',img_info)\n",
    "            \n",
    "\n",
    "            \n",
    "            # shutil.copy(join(self.src, self.obj_lib[idx]), tgt_path)\n",
    "            shutil.copy2(join(self.src, self.obj_lib[idx]), join(tgt_path,'_'.join(img_info)))\n",
    "            \n",
    "            \n",
    "\n",
    "        \n",
    "QueryGenerator(tgt_path,src_path)\n",
    "\n",
    "    \n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yang_real",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
